home *** CD-ROM | disk | FTP | other *** search
- // ******************************************************************
- // program written by
- // Paul Baxter
- // MacHack'98
- //
- // ******************************************************************
-
- #include <A4Stuff.h>
- #include <stdlib.h>
- #include "ShowInit.h"
- #include "patch.h"
-
- //#include "debug.h"
-
- #define kIconID 5000 // Install Icon
- #define kNoInstallIconID 5001 // Can't Install Icon
-
- #define kSndPlayer68K 'SD68' // 68K PlaySound Resource type
- #define kSndPlayerPPC 'SDPP' // PPC PlaySound Resource type
- #define kShuffle 10000 // Number of times randomize sound play order
-
- OSErr InstallSound(void); // Installs sounds and sound function
- OSErr InstallPatches(void); // Installs the patches
- short myRandom(short min, short max ); // Random function that takes a min and max
-
- PlaySoundUPP gSoundPlayer68K = nil; // 68K PlaySound Function
- PlaySoundUPP gSoundPlayerPPC = nil; // PPC PlaySound Function
- short gNumSounds = 0; // Number of sounds
- short *gSoundIndex = nil; // array holding sound play order
- SndListHandle* gMySounds = nil; // array of sounds
- short gOurResFile; // resfile of the INIT
-
- // * ******************************************************************************
- // * main
- // * Entry Point for the extension
- // * ******************************************************************************
- void main(void)
- {
- THz theZone;
- OSErr err;
-
- EnterCodeResource();
-
- gOurResFile = CurResFile();
- err = noErr;
-
- theZone = GetZone();
- SetZone(SystemZone());
-
- if (!err)
- err = InstallSound();
-
- // DEBUG("InstallSound return %d", err);
-
- if (!err)
- err = InstallPatches();
-
- // DEBUG("InstallPatches return %d", err);
- if (!err)
- ShowINIT(kIconID, -1);
- else
- ShowINIT(kNoInstallIconID, -1);
-
- // DEBUG("Exit INIT");
-
- SetZone(theZone);
- ExitCodeResource();
- }
-
- // * ******************************************************************************
- // * InstallSound
- // * Load sounds and sound player functions
- // InstallSound Must be called before InstallPatches
- // * ******************************************************************************
- OSErr InstallSound(void)
- {
- Handle theHandle;
- SoundInstallerPtr soundInstallCode;
- ResType theType;
- long result;
- short randcount, index, snd1, snd2, temp;
- OSErr err;
-
- // DEBUG("Enter InstallSound");
-
- gNumSounds = Count1Resources('snd ');
- gSoundIndex = (short*)NewPtrSys(gNumSounds * sizeof(short));
- if (gSoundIndex) {
- gMySounds = (SndListHandle* )NewPtrSys(gNumSounds * sizeof(SndListHandle));
- if (gMySounds) {
- for (index = 0; index < gNumSounds; index++) {
- gSoundIndex[index] = index;
- UseResFile(gOurResFile);
- gMySounds[index] = (SndListHandle) Get1IndResource('snd ', index + 1);
- if (gMySounds[index]) {
- HLockHi((Handle)gMySounds[index]);
- DetachResource((Handle)gMySounds[index]);
- }
- }
-
- // lets play the sounds in a random order
- for (randcount = 0; randcount < kShuffle; randcount++) {
- snd1 = myRandom(0, gNumSounds);
- snd2 = myRandom(0, gNumSounds);
-
- temp = gSoundIndex[snd1];
- gSoundIndex[snd1] = gSoundIndex[snd2];
- gSoundIndex[snd2] = temp;
- }
- }
- }
-
- err = Gestalt(gestaltSysArchitecture, &result);
- if ((!err) && (result == gestaltPowerPC)) {
- theType = kSndPlayerPPC;
- UseResFile(gOurResFile);
- theHandle = Get1IndResource(theType, 1);
- if (theHandle) {
- HLockHi(theHandle);
- DetachResource(theHandle);
- soundInstallCode = NEW_SOUND_INSTALLER_PROC(*theHandle);
- if (soundInstallCode) {
- gSoundPlayerPPC = CALL_SOUND_INSTALLER_PROC(soundInstallCode, gNumSounds, gSoundIndex, gMySounds);
- }
- }
- }
-
- theType = kSndPlayer68K;
- UseResFile(gOurResFile);
- theHandle = Get1IndResource(theType, 1);
- if (theHandle) {
- HLockHi(theHandle);
- DetachResource(theHandle);
- soundInstallCode = NEW_68K_SOUND_INSTALLER_PROC(*theHandle);
- if (soundInstallCode) {
- gSoundPlayer68K = CALL_SOUND_INSTALLER_PROC(soundInstallCode, gNumSounds, gSoundIndex, gMySounds);
- }
- }
-
- // DEBUG("Exit InstallSound");
-
- if (gSoundPlayer68K || gSoundPlayerPPC) {
- return noErr;
- }
- return -1;
- }
-
- // * ******************************************************************************
- // * InstallPatches
- // * Loads the patches
- // InstallSound Must be called before InstallPatches
- // * ******************************************************************************
- OSErr InstallPatches()
- {
- Handle theHandle;
- PatchInstallerUPP patchInstallCode;
- ProcPtr theProc;
- Str255 rName;
- ResType theType;
- long result;
- short count, index, rID;
- OSErr err;
-
- // DEBUG("Enter InstallPatches");
-
- err = Gestalt(gestaltSysArchitecture, &result);
- if ((!err) && (result == gestaltPowerPC))
- theType = kPatchTypePPC;
- else
- theType = kPatchType68K;
-
- UseResFile(gOurResFile);
- count = Count1Resources(theType);
- if (count == 0 && theType == kPatchTypePPC) {
- theType = kPatchType68K;
- UseResFile(gOurResFile);
- count = Count1Resources(theType);
- }
- for (index = 1; index <= count; index++) {
- UseResFile(gOurResFile);
- theHandle = Get1IndResource(theType, index);
- if (theHandle) {
- GetResInfo(theHandle, &rID, &theType, rName);
- HLockHi(theHandle);
- DetachResource(theHandle);
- patchInstallCode = nil;
- #ifdef powerc
- if (theType == kPatchTypePPC)
- patchInstallCode = NEW_PATCH_INSTALLER_PROC(*theHandle);
- #endif
- if (!patchInstallCode)
- patchInstallCode = NEW_68K_PATCH_INSTALLER_PROC(*theHandle);
-
- if (patchInstallCode) {
- UseResFile(gOurResFile);
- theProc = CALL_PATCH_INSTALLER_PROC(patchInstallCode, gSoundPlayer68K, gSoundPlayerPPC, rID, true);
- }
- }
- }
- UseResFile(gOurResFile);
-
- // DEBUG("Exit InstallPatches");
-
- return noErr;
- }
-
- // * ******************************************************************************
- // * myRandom
- // * generate random numbers with a min and max
- // * ******************************************************************************
- short myRandom(short min, short max )
- {
- unsigned short rnd;
- long range, t;
-
- rnd = rand();
- range = max - min;
- t = (rnd * range) / RAND_MAX;
- return( t+min );
- }